package net.yb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import net.yb.entity.User;
import net.yb.mapper.UserMapper;
import net.yb.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    /**
     * 根据id删除用户
     */
    @Override
    public void removeById(int id) {
        userMapper.deleteById(id);

        int i = 5/0;
    }


    @Override
    public PageInfo<User> queryByPage(Integer pageNum, Integer pageSize) {
        // 1.开启分页
        PageHelper.startPage(pageNum, pageSize);

        // 2.查询数据
        List<User> list = userMapper.selectAll();

        // 3.封装分页数据
        PageInfo<User> pageInfo = new PageInfo<>(list); // 传入上一步获取的list

        return pageInfo;
    }
}
